﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ttbit.form.impl
{
    internal class Env_OTK : IEnv_Impl
    {
        public Env_OTK(IPlatform platform)
        {
            this.Platform = platform;
            this.Graphic = new Graphics();
            this.Input = new Input();
            this.Audio = new Audio();
            //this.FileIO = new FileIO()
            //{
            //    InternalResourcePath = FindResourcesPath()
            //};

            if (System.IO.File.Exists("index.ttpak"))
            {
                var fs = System.IO.File.OpenRead("index.ttpak");
                this.RootPack = new PackGroup_Stream("root",fs);
            }
            else
            {
                var path = FindResourcesPath();
                this.RootPack = new PackGroup_Folder("root",path);
            }

        }
        string FindResourcesPath()
        {

            string pathend = "resources";
            string fullpath = System.IO.Path.GetFullPath("./");
            while (true)
            {
                var path = System.IO.Path.Combine(fullpath, pathend);
                if (System.IO.Directory.Exists(path))
                {
                    var tagfile = System.IO.Path.Combine(path, "ttpak.json");
                    if(System.IO.File.Exists(tagfile))
                    return path;
                }
                fullpath = System.IO.Path.GetDirectoryName(fullpath);
                if (string.IsNullOrEmpty(fullpath))
                {
                    throw new Exception("can not find resources.");
                }
            }
        }
        public IPlatform Platform
        {
            get;
            private set;
        }

        IGraphic IEnv_Impl.Graphic => Graphic;
        public impl.Graphics  Graphic
        {
            get;
            private set;
        }
        public IInput Input
        {
            get;
            private set;
        }

        //public IFileIO FileIO
        //{
        //    get;
        //    private set;
        //}
        public IPackGroup RootPack
        {
            get;
            private set;
        }
        public IAudio Audio
        {
            get;
            private set;
        }



        public void Log(string message, LogLevel level = LogLevel.Normal)
        {
            Debug.WriteLine(message, level);
        }
    }
}
